{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-03-19T12:02:09.572883600Z",
     "start_time": "2024-03-19T12:02:09.483816Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "['test.ini']"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import configparser\n",
    "config = configparser.ConfigParser()\n",
    "config.read('test.ini', encoding='utf-8')\n"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['default', 'mysql']\n"
     ]
    }
   ],
   "source": [
    "print(config.sections())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-19T12:02:09.576894100Z",
     "start_time": "2024-03-19T12:02:09.543522400Z"
    }
   },
   "id": "bcf312196749b774",
   "execution_count": 10
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "azmat\n"
     ]
    }
   ],
   "source": [
    "print(config.get('default', 'name'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-19T12:02:09.628859Z",
     "start_time": "2024-03-19T12:02:09.581979700Z"
    }
   },
   "id": "16abb60c5c98a3bc",
   "execution_count": 11
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "20 <class 'int'>\n"
     ]
    }
   ],
   "source": [
    "print(config.getint('default', 'age'),type(config.getint('default', 'age')))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-19T12:02:09.665621900Z",
     "start_time": "2024-03-19T12:02:09.631837200Z"
    }
   },
   "id": "3007d2ca62547d52",
   "execution_count": 12
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(config.getboolean('default', 'rd'))\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-19T12:02:09.700498400Z",
     "start_time": "2024-03-19T12:02:09.669625500Z"
    }
   },
   "id": "1688f8ed0b48964",
   "execution_count": 13
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.55\n"
     ]
    }
   ],
   "source": [
    "print(config.getfloat('default', 'cha'))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-19T12:02:09.728706500Z",
     "start_time": "2024-03-19T12:02:09.702497500Z"
    }
   },
   "id": "f64f99c02c23c9ab",
   "execution_count": 14
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "config.set('default', 'name', '张三')\n",
    "with open('test.ini', 'w', encoding='utf-8') as configfile:\n",
    "    config.write(configfile)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-19T12:05:12.688649800Z",
     "start_time": "2024-03-19T12:05:12.632164700Z"
    }
   },
   "id": "a38373b73a68f83a",
   "execution_count": 20
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "ename": "DuplicateSectionError",
     "evalue": "Section 'new' already exists",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mDuplicateSectionError\u001B[0m                     Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[18], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m \u001B[43mconfig\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43madd_section\u001B[49m\u001B[43m(\u001B[49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[38;5;124;43mnew\u001B[39;49m\u001B[38;5;124;43m'\u001B[39;49m\u001B[43m)\u001B[49m\n\u001B[0;32m      2\u001B[0m config\u001B[38;5;241m.\u001B[39mset(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mnew\u001B[39m\u001B[38;5;124m'\u001B[39m, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mname\u001B[39m\u001B[38;5;124m'\u001B[39m, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mna\u001B[39m\u001B[38;5;124m'\u001B[39m)\n\u001B[0;32m      3\u001B[0m \u001B[38;5;28;01mwith\u001B[39;00m \u001B[38;5;28mopen\u001B[39m(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mtest.ini\u001B[39m\u001B[38;5;124m'\u001B[39m, \u001B[38;5;124m'\u001B[39m\u001B[38;5;124mw\u001B[39m\u001B[38;5;124m'\u001B[39m) \u001B[38;5;28;01mas\u001B[39;00m configfile:\n",
      "File \u001B[1;32mD:\\Apply\\Anaconda3\\envs\\WeChat\\Lib\\configparser.py:1227\u001B[0m, in \u001B[0;36mConfigParser.add_section\u001B[1;34m(self, section)\u001B[0m\n\u001B[0;32m   1223\u001B[0m \u001B[38;5;250m\u001B[39m\u001B[38;5;124;03m\"\"\"Create a new section in the configuration.  Extends\u001B[39;00m\n\u001B[0;32m   1224\u001B[0m \u001B[38;5;124;03mRawConfigParser.add_section by validating if the section name is\u001B[39;00m\n\u001B[0;32m   1225\u001B[0m \u001B[38;5;124;03ma string.\"\"\"\u001B[39;00m\n\u001B[0;32m   1226\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_validate_value_types(section\u001B[38;5;241m=\u001B[39msection)\n\u001B[1;32m-> 1227\u001B[0m \u001B[38;5;28;43msuper\u001B[39;49m\u001B[43m(\u001B[49m\u001B[43m)\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43madd_section\u001B[49m\u001B[43m(\u001B[49m\u001B[43msection\u001B[49m\u001B[43m)\u001B[49m\n",
      "File \u001B[1;32mD:\\Apply\\Anaconda3\\envs\\WeChat\\Lib\\configparser.py:674\u001B[0m, in \u001B[0;36mRawConfigParser.add_section\u001B[1;34m(self, section)\u001B[0m\n\u001B[0;32m    671\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mValueError\u001B[39;00m(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mInvalid section name: \u001B[39m\u001B[38;5;132;01m%r\u001B[39;00m\u001B[38;5;124m'\u001B[39m \u001B[38;5;241m%\u001B[39m section)\n\u001B[0;32m    673\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m section \u001B[38;5;129;01min\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_sections:\n\u001B[1;32m--> 674\u001B[0m     \u001B[38;5;28;01mraise\u001B[39;00m DuplicateSectionError(section)\n\u001B[0;32m    675\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_sections[section] \u001B[38;5;241m=\u001B[39m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_dict()\n\u001B[0;32m    676\u001B[0m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_proxies[section] \u001B[38;5;241m=\u001B[39m SectionProxy(\u001B[38;5;28mself\u001B[39m, section)\n",
      "\u001B[1;31mDuplicateSectionError\u001B[0m: Section 'new' already exists"
     ]
    }
   ],
   "source": [
    "config.add_section('new')\n",
    "config.set('new', 'name', 'na')\n",
    "with open('test.ini', 'w') as configfile:\n",
    "    config.write(configfile)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-19T12:04:42.536215500Z",
     "start_time": "2024-03-19T12:04:42.506469600Z"
    }
   },
   "id": "3bebc633e50a52da",
   "execution_count": 18
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "config.remove_section('new_section')\n",
    "config.remove_option('new', 'name')\n",
    "with open('test.ini', 'w', encoding='utf-8') as configfile:\n",
    "    config.write(configfile)\n"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-19T12:06:09.759217700Z",
     "start_time": "2024-03-19T12:06:09.702076700Z"
    }
   },
   "id": "42d63f3be5f85faf",
   "execution_count": 21
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [
    "config.remove_section('new_1')\n",
    "config.remove_section('new')\n",
    "with open('test.ini', 'w', encoding='utf-8') as configfile:\n",
    "    config.write(configfile)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-03-19T12:06:54.554778200Z",
     "start_time": "2024-03-19T12:06:54.504927200Z"
    }
   },
   "id": "1fd62dcc3163af9c",
   "execution_count": 22
  },
  {
   "cell_type": "code",
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "846e183ab8f601b0"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
